package com.example.flower_shop.service.adm.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.entity.Article;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.ArticleMapper;
import com.example.flower_shop.service.adm.AdmArticleService;
import com.example.flower_shop.vo.article.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdmArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
        implements AdmArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public PageResult<ArticlePageResp> findAllArticle(ArticlePageReq req) {
        Page<ArticlePageResp> page = new Page<>(req.getPage(), req.getSize());
        IPage<ArticlePageResp> iPage = articleMapper.pageQuery(page, req);
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    @Transactional(rollbackFor = exception.class)
    public void updateArticle(AdmArticleUpdateReq req) {
        Article one = getById(req.getId());
        if (one == null) {
            throw new exception("文章不存在");
        }
        one.setStatus(!one.getStatus());
        if (articleMapper.updateById(one) < 1) {
            throw new exception("更新失败");
        }
    }

    @Override
    public void batchDelete(Long[] ids) {
        removeByIds(List.of(ids));
    }

    @Override
    @Transactional(rollbackFor = exception.class)
    public void updateArticleInfo(ArticleReq req) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getId, req.getId());
        Article article = getOne(wrapper);
        if (article == null) {
            throw new exception("文章不存在");
        }
        Article newArticle = BeanUtil.copyProperties(req, Article.class);
        // 证明有新图片要上传
        if (req.getImageInput() != null && req.getImageInput().length > 0) {
            newArticle.setImageInput(transferImage(req.getImageInput()));
        }
        try {
            if (!updateById(newArticle)) {
                throw new exception("文章更新失败");
            }
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = exception.class)
    public void createArticle(ArticleReq req) {
        Article article = BeanUtil.copyProperties(req, Article.class);
        article.setUid(StpUtil.getLoginIdAsLong());
        if (req.getImageInput() != null && req.getImageInput().length > 0) {
            article.setImageInput(transferImage(req.getImageInput()));
        }
        try {
            if (!save(article)) {
                throw new exception("文章保存失败");
            }
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public ArticleRespVO getArticle(Long articleId) {
        ArticleRespVO article = articleMapper.getArticle(articleId);
        if (article == null) {
            throw new exception("文章不存在");
        }
        return article;
    }

    private String transferImage(String[] images) {
        return JSONUtil.toJsonStr(images[1].contains("base64") ?
                List.of(images) : List.of(images[0] + "," + images[1]));
    }
}
